package com.studiocode.aspeaker;

import com.android.internal.telephony.ITelephony;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaRecorder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyPhoneStateListener extends PhoneStateListener
{
	public final String TAGDEBUG = "aSpeaker";
	
	private Context myContext;
	private AudioManager m_audioManager;

	final MediaRecorder recorder = new MediaRecorder();
	private int RingerMode = 0;
	private ITelephony m_telephonyService;
	private boolean CanSpeak = false;
	private boolean SpeakOnlyRingMode = true;
	
	public void setSpeakOnlyRingMode(boolean speakOnlyRingMode) {
		SpeakOnlyRingMode = speakOnlyRingMode;
	}

	public MyPhoneStateListener(Context superContext, AudioManager audioManager, 
			ITelephony m_telephonyservice)
	{
		myContext = superContext;
		m_audioManager = audioManager;
		m_telephonyService = m_telephonyservice;
	}
	
    public void setCanSpeak(boolean canSpeak) {
		CanSpeak = canSpeak;
	}

	public void onCallStateChanged(int state, String incomingNumber)
    {
    	RingerMode = m_audioManager.getRingerMode();
    	
        switch (state)
        {
            case TelephonyManager.CALL_STATE_IDLE:
               //CALL_STATE_IDLE

               break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
               //CALL_STATE_OFFHOOK;
			    if (m_audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
			    {
    		   		m_audioManager.adjustVolume(AudioManager.ADJUST_SAME, AudioManager.FLAG_ALLOW_RINGER_MODES);
			    }
               break;
            case TelephonyManager.CALL_STATE_RINGING:
        	   try
        	   {   
        		   	Log.v(TAGDEBUG, "Ringing!");

        		   	//m_telephonyService.silenceRinger();
        		    String Caller = incomingNumber;
        		    Log.v(TAGDEBUG, "incomingNumber = " + incomingNumber);
        		    if ((Caller == null) || (Caller.equals("")))
        		    	Caller = myContext.getString(R.string.speakSconosciuto);
        		    else
        		    {
        		    	ContactsUtils myCU = new ContactsUtils();
        		    	try
        		    	{
        		    		Caller = myCU.NameFromNumber(myContext, Caller);
        		    	}
        		    	catch (Exception ex)
        		    	{
        		    		Log.v(TAGDEBUG, "Errore: " + ex.getMessage().toString());
        		    	}
        		    }
        		    Log.v(TAGDEBUG, "Chiamata in arrivo da "+Caller);
        		    //Bene, adesso lo parlo!!
				    if ((m_audioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) ||
				    		(SpeakOnlyRingMode == false))
				    {
        		   		m_audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_ALLOW_RINGER_MODES);
        		   		//adjustStreamVolume(AudioManager.STREAM_RING, 
        		   		//	AudioManager.ADJUST_LOWER, AudioManager.FLAG_ALLOW_RINGER_MODES);
				    	TTS myTTS5 = new TTS(myContext,myContext.getString(R.string.speakChiamataInArrivoDa) + Caller,CanSpeak);
        	   		}
        	   }
        	   catch (Exception ex)
        	   {
        		   Log.v(TAGDEBUG, "Errore: " + ex.getMessage().toString());
        	   }
               break;
            default:
               break;
         }
    }
}
